package cf.luobo.covid_19.action;

import cf.luobo.covid_19.javaBean.pojo.ErrMsg;
import cf.luobo.covid_19.javaBean.pojo.Users;
import cf.luobo.covid_19.service.UsersService;
import cf.luobo.covid_19.service.imp.UsersServiceImp;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/users")
public class UsersServlet extends HttpServlet {
    private HttpServletRequest request;
    private HttpServletResponse response;

    private UsersService service = new UsersServiceImp();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.request = req;
        this.response = resp;

        String method = request.getParameter("method"); //获取一个请求的数据

        if (method.equals("updateUsers")) {
            this.updateUsers();
        } else if (method.equals("addUsers")) {
            this.addUsers();
        } else if (method.equals("Login")) {
            this.Login();
        }
    }

    //登录
    private void Login() throws IOException {
        response.setContentType("text/json; charset=utf-8");    // 设置response的编码及格式
        PrintWriter out = response.getWriter();
        ErrMsg errMsg = new ErrMsg(404, "Unknown Error");
        int id = Integer.parseInt(request.getParameter("id"));
        String pwd = request.getParameter("pwd");
        Users user = service.getUsers(id);
        if (user == null) {
            errMsg.setErrcode(10);
            errMsg.setErrmsg("用户不存在！");
        } else if (!user.getPwd().equals(pwd)) {
            errMsg.setErrcode(11);
            errMsg.setErrmsg("密码错误！");
        } else if (user.getPwd().equals(pwd) && user.getAdmin()) {
            errMsg.setErrcode(1);
            errMsg.setErrmsg("管理员");
        } else if (user.getPwd().equals(pwd) && !user.getAdmin()) {
            errMsg.setErrcode(2);
            errMsg.setErrmsg("用户");
        }
        String text = JSON.toJSONString(errMsg);
        out.print(text); // 输出
        out.flush();
    }

    //插入用户数据
    private void addUsers() throws IOException {
        response.setContentType("text/json; charset=utf-8");    // 设置response的编码及格式
        PrintWriter out = response.getWriter();
        int id = Integer.parseInt(request.getParameter("id"));
        String uname = request.getParameter("uname");
        String pwd = request.getParameter("pwd");
        int age = Integer.parseInt(request.getParameter("age"));
        String sex = request.getParameter("sex");
        String s_test = request.getParameter("admin");
        boolean admin = Boolean.parseBoolean(s_test);
        Users users = new Users(id, uname, pwd, age, sex, admin);
        ErrMsg errmsg = service.saveUsers(users);
        String text = JSON.toJSONString(errmsg);
        out.print(text); // 输出
        out.flush();
    }

    //更新用户数据
    private void updateUsers() throws IOException {
        response.setContentType("text/json; charset=utf-8");    // 设置response的编码及格式
        PrintWriter out = response.getWriter();
        int id = Integer.parseInt(request.getParameter("id"));
        String uname = request.getParameter("uname");
        String pwd = request.getParameter("pwd");
        int age = Integer.parseInt(request.getParameter("age"));
        String sex = request.getParameter("sex");
        String s_test = request.getParameter("admin");
        boolean admin = Boolean.parseBoolean(s_test);
        Users users = new Users(id, uname, pwd, age, sex, admin);
        ErrMsg errmsg = service.updateUser(users);
        String text = JSON.toJSONString(errmsg);
        out.print(text); // 输出
        out.flush();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
